package org.example.travel_web.controller.admin;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.example.travel_web.dto.PageResult;
import org.example.travel_web.dto.news.NewsDetail;
import org.example.travel_web.dto.news.NewsReq;
import org.example.travel_web.dto.news.PageReq;
import org.example.travel_web.dto.news.PageResp;
import org.example.travel_web.entity.NewsCategoriesET;
import org.example.travel_web.service.NewsCategoriesService;
import org.example.travel_web.service.NewsService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/news")
@Tag(name = "资讯管理")
public class NewsController {

    @Resource
    private NewsService newsService;
    @Resource
    private NewsCategoriesService newsCategoriesService;

    @GetMapping
    @Operation(summary = "资讯分页")
    public PageResult<PageResp> newsPageResult(@Valid PageReq req) {
        return newsService.pageResult(req);
    }

    @GetMapping("/category")
    @Operation(summary = "获取资讯分类")
    public List<NewsCategoriesET> getNewsCategories() {
        return newsCategoriesService.list();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取资讯详情")
    @Parameter(name = "id", description = "资讯 ID", required = true, in = ParameterIn.PATH)
    public NewsDetail getNewsDetail(@PathVariable Integer id) {
        return newsService.getNewsDetail(id);
    }

    @PostMapping
    @Operation(summary = "添加资讯")
    public void addNews(@Valid NewsReq req) {
        newsService.addNews(req);
    }

    @PutMapping
    @Operation(summary = "修改资讯")
    public void updateNews(@Valid NewsReq req) {
        newsService.updateNews(req);
    }

    @DeleteMapping
    @Operation(summary = "删除资讯")
    @Parameter(name = "ids", description = "资讯 ID 数组", required = true, in = ParameterIn.QUERY)
    public void deleteNews(@RequestParam Integer[] ids) {
        newsService.deleteNews(ids);
    }
}
